package com.pllandxzx.service.impl;

import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pll.dto.ResponseDto;
import com.pll.pojo.Menu;
import com.pll.pojo.Role;
import com.pllandxzx.bean.MenuBean;
import com.pllandxzx.dto.ResponseDTO;
import com.pllandxzx.mapper.MenuMapper;
import com.pllandxzx.mapper.RoleMapper;
import com.pllandxzx.service.RoleService;
import com.pllandxzx.tool.BuildMenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName：RoleServiceImpl
 * @Author: 阿⭐
 * @Date: 2025/6/10 10:34
 * @Description: TODO
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private BuildMenuTree buildMenuTree;

    @Override
    public ResponseDTO  getAllRole() {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return ResponseDTO.Success(roleMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public ResponseDTO addRole(Role role) {
        //查询判断数据库中是否有添加的角色
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getRoleName,role.getRoleName());
        List<Role> roles = roleMapper.selectList(lambdaQueryWrapper);
        if (!roles.isEmpty()){
            return ResponseDTO.Fail(400,"角色已存在");
        }
        //插入数据库并返回插入条数
        return ResponseDTO.Success(roleMapper.insert(role));
    }

    @Override
    public ResponseDTO updateRole(Role role) {
        //查询判断数据库中是否有更新的角色名称
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Role::getRoleName,role.getRoleName());
        List<Role> roles = roleMapper.selectList(lambdaQueryWrapper);
        if (!roles.isEmpty()){
            return ResponseDTO.Fail(400,"该角色已存在");
        }
        System.out.println("=============="+role);
        //更新数据库并返回更新条数
        return ResponseDTO.Success(roleMapper.updateById(role));
    }

    @Override
    public ResponseDTO deleteRole(int roleId) {
        List<MenuBean> menuByRoleId = menuMapper.getMenuByRoleId(roleId);
        if (!menuByRoleId.isEmpty()){
            return ResponseDTO.Fail(400,"该角色已分配权限，请先删除权限");
        }
        return ResponseDTO.Success(roleMapper.deleteById(roleId));
    }

    @Override
    public ResponseDTO updateRolePermission(int roleId, List<Integer> menuIds) {
        //删除该角色的所有权限
        roleMapper.deleteRolePermission(roleId);
        if (menuIds.isEmpty()){
            return ResponseDTO.Success(2000,"已删除该角色所有权限");
        }
        //添加该角色的权限
        return ResponseDTO.Success(roleMapper.putRolePermission(roleId,menuIds));
    }

    @Override
    public ResponseDTO getRoleMenu(long roleId) {
        List<MenuBean> menuByRoleId = menuMapper.getMenuByRoleId(roleId);
        menuByRoleId = buildMenuTree.buildMenuTree(menuByRoleId);
        return ResponseDTO.Success(menuByRoleId);
    }
}
